//
//  BenchmarkTestCell.m
//  GCDBenchmark
//
//  Created by Claude on 2025/10/13.
//

#import "BenchmarkTestCell.h"

@interface BenchmarkTestCell ()
@property (nonatomic, strong) UILabel *iconLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *paramLabel;
@property (nonatomic, strong) UIButton *configButton;
@property (nonatomic, strong) NSLayoutConstraint *configWidthConstraint;
@property (nonatomic, strong) BenchmarkTestCase *testCase;
@end

@implementation BenchmarkTestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleDefault;
    self.accessoryType = UITableViewCellAccessoryNone;

    // 图标标签
    self.iconLabel = [[UILabel alloc] init];
    self.iconLabel.font = [UIFont systemFontOfSize:24];
    self.iconLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.iconLabel];

    // 名称标签
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    self.nameLabel.numberOfLines = 0;
    self.nameLabel.textColor = [UIColor labelColor];
    self.nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.nameLabel];

    // 参数标签
    self.paramLabel = [[UILabel alloc] init];
    self.paramLabel.font = [UIFont systemFontOfSize:13];
    self.paramLabel.textColor = [UIColor secondaryLabelColor];
    self.paramLabel.numberOfLines = 0;
    self.paramLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.paramLabel];

    self.configButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.configButton setTitle:@"配置" forState:UIControlStateNormal];
    self.configButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
    self.configButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.configButton addTarget:self action:@selector(configButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.configButton];

    self.configWidthConstraint = [self.configButton.widthAnchor constraintEqualToConstant:44];
    self.configWidthConstraint.active = YES;

    NSLayoutConstraint *paramToButton = [self.paramLabel.trailingAnchor constraintLessThanOrEqualToAnchor:self.configButton.leadingAnchor constant:-12];
    paramToButton.priority = UILayoutPriorityRequired - 2;

    [NSLayoutConstraint activateConstraints:@[
        [self.iconLabel.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:16],
        [self.iconLabel.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor],
        [self.iconLabel.widthAnchor constraintEqualToConstant:30],

        [self.configButton.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:8],
        [self.configButton.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-16],
        [self.configButton.heightAnchor constraintEqualToConstant:32],

        [self.nameLabel.leadingAnchor constraintEqualToAnchor:self.iconLabel.trailingAnchor constant:12],
        [self.nameLabel.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:12],
        [self.nameLabel.trailingAnchor constraintLessThanOrEqualToAnchor:self.configButton.leadingAnchor constant:-12],

        [self.paramLabel.leadingAnchor constraintEqualToAnchor:self.nameLabel.leadingAnchor],
        [self.paramLabel.topAnchor constraintEqualToAnchor:self.nameLabel.bottomAnchor constant:4],
        paramToButton,
        [self.paramLabel.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-16],
        [self.paramLabel.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-12],
    ]];
}

- (void)configureWithTestCase:(BenchmarkTestCase *)testCase {
    self.testCase = testCase;
    self.iconLabel.text = testCase.icon;
    self.nameLabel.text = testCase.name;
    self.paramLabel.text = testCase.parameterDescription;

    BOOL shouldShowConfig = (testCase.type != BenchmarkTestCaseTypePlayback);
    self.configButton.hidden = !shouldShowConfig;
    self.configButton.enabled = shouldShowConfig;
    self.configWidthConstraint.constant = shouldShowConfig ? 44 : 0;
    self.configButton.alpha = shouldShowConfig ? 1.0 : 0.0;
}

- (void)configButtonTapped {
    if (!self.configButton.hidden && [self.delegate respondsToSelector:@selector(benchmarkTestCellDidTapConfig:)]) {
        [self.delegate benchmarkTestCellDidTapConfig:self.testCase];
    }
}

@end
